package problem129;

//129.根节点到叶子节点数字之和
//https://leetcode.cn/problems/sum-root-to-leaf-numbers/

class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }
    public int dfs(TreeNode root, int n) {
        n = n * 10 + root.val;
        int left = 0, right = 0;
        if(root.left == null && root.right == null) return n;
        if(root.left != null) {
            left = dfs(root.left, n);
        }
        if(root.right != null) {
            right = dfs(root.right, n);
        }
        return left + right;
    }
}